﻿using System;
using System.Diagnostics;

namespace ProcessMonitor {
  class HeapMonitor {
    public static void Main(string[] args) {
      string argument = "";
      for (int i = 1; i < args.Length; i++) {
        argument += args[i] + " ";
      }
      ProcessStartInfo info = new ProcessStartInfo(args[0]);
      info.UseShellExecute = false;
      info.RedirectStandardOutput = true;
      info.Arguments = argument;
      Process p = Process.Start(info);

      long peakPagedMemorySize = 0;
      long peakVirtualMemorySize = 0;
      long peakWorkingSet = 0;
      while (!p.HasExited) {
        p.Refresh();
        peakPagedMemorySize = p.PeakPagedMemorySize64;
        peakVirtualMemorySize = p.PeakVirtualMemorySize64;
        peakWorkingSet = p.PeakWorkingSet64;
      }
      Console.WriteLine("PeakWorkingSet        : " +
          peakWorkingSet.ToString("###,###") + " bytes");
      Console.WriteLine("PeakPagedMemorySize   : " +
          peakPagedMemorySize.ToString("###,###") + " bytes");
      Console.WriteLine("PeakVirtualMemorySize : " +
          peakVirtualMemorySize.ToString("###,###") + " bytes");
    }
  }
}
